package liming.ai.code.v3.data;

import liming.ai.code.v3.core.ChatCore;
import liming.ai.data.deepseek.ChatCompletionRequest;
import liming.tool.file.FileRW;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JavaCode {
    String packageName;
    String className;
    List<String> imports;
    String md;
    String code;
    String cmd;
    public JavaCode(String packageName, String className, List<String> imports, String code) {
        this.packageName = packageName==null?"":packageName;
        this.className = className==null?"":className;
        this.imports = imports==null?new ArrayList<>():imports;
        this.code = code==null?"":code;
    }

    public String getPackageName() {
        return packageName;
    }

    public String getClassName() {
        return className;
    }
    public String getMd() {
        return md;
    }
    public String getCmd() {
        return cmd;
    }

    public String getCode() {
        return code;
    }

    public static List<JavaCode> think(JavaCode javaCode,Context context) {
        ChatCompletionRequest request = ChatCore.buildQueryPrompt();
        String chat = ChatCore.chat("辅助文件："+context.getContext()+"\n请根据辅助文件，联系上下文，将以下内容生成完整的java代码,包含包名、导入其他的路径、类名，并删除其中多余的代码：\n" + javaCode, request);
        JSONObject json = ChatCore.valueToJson(chat);
        while (json.has("files")){
            Object fs = json.get("files");
            if(fs instanceof JSONArray){
                StringBuilder sb = new StringBuilder();
                JSONArray files= (JSONArray) fs;
                if(files.isEmpty())
                    break;
                for (Object o : files) {
                    if(o instanceof String){
                        sb.append("文件:").append(o).append(" 内容:\n");
                        try {
                            sb.append(FileRW.readTextFile(new File((String) o))).append("\n");
                        } catch (IOException e) {
                            String stackTrace = FileRW.getStackTrace(e);
                            sb.append("文件读取失败：").append(o).append("\n").append(stackTrace);
                            System.out.println("文件读取失败："+o+" "+ stackTrace);
                        }
                    }
                }
                json = ChatCore.valueToJson(ChatCore.chat("所需推展文件内容:"+sb+"\n请联系上下文，将生成完整的java代码,包含包名、导入其他的路径、类名，并删除其中冗余代码\n", request));
            }
        }
        return loads(json.getJSONArray("classes"));
    }

    public static List<JavaCode> loads(JSONArray classes){
        List<JavaCode> list = new ArrayList<>();
        for (Object o : classes)
            if(o instanceof JSONObject)
                list.add(load((JSONObject) o));
        return list;
    }
    public static JavaCode load(JSONObject jsonObject){
        List<String> imports = new ArrayList<>();
        if(jsonObject.has("imports")){
            if(jsonObject.get("imports") instanceof JSONArray){
                for (Object o : jsonObject.getJSONArray("imports")) {
                    if(o instanceof String)
                        imports.add(o.toString());
                }
            }
            else if(jsonObject.get("imports") instanceof String)
                imports.add(jsonObject.getString("imports"));
        }
        JavaCode javaCode = new JavaCode(jsonObject.optString("package"), jsonObject.optString("name"), imports, jsonObject.optString("code"));
        if(jsonObject.has("md"))
            javaCode.md = jsonObject.opt("md").toString();
        return javaCode;
    }

    @Override
    public String toString() {
        return "JavaCode{" +
                "packageName=\n\t" + packageName + '\n' +
                "className=\n\t" + className + '\n' +
                "imports=\n\t" + imports + '\n' +
                "code=\n\t" + code + '\n' +
                '}';
    }
}
